package com.AuthServer.config.handler;

import com.AuthServer.common.Result;
import com.AuthServer.exception.*;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.AddressException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BindException.class)
    public Result<String> handleBindException(BindException e) {
        List<FieldError> allErrors = e.getFieldErrors();
        String errorMessage = allErrors.stream()
                .map(error -> error.getField() + ": " + error.getDefaultMessage())
                .collect(Collectors.joining(", "));
        return Result.fail(100, "参数不合法", errorMessage);
    }

    @ExceptionHandler(LoginStatusInvalidException.class)
    public Result<?> handleLoginStatusInvalidException(){
        return Result.fail(101,"登录状态失效",null);
    }

    @ExceptionHandler({UsernameNotFoundException.class,AuthenticationException.class})
    public Result<?> handleAuthException(){
        return Result.fail(102,"用户名或密码错误",null);
    }

    @ExceptionHandler(IOException.class)
    public Result<?> handleIOException(){
        return Result.fail(103,"获取图片验证码失败",null);
    }

    @ExceptionHandler(CaptchaIncorrectException.class)
    public Result<?> handleCaptchaIncorrectException(){
        return Result.fail(104,"验证码错误",null);
    }

    @ExceptionHandler(CaptchaExpiredException.class)
    public Result<?> handleCaptchaExpiredException(){
        return Result.fail(105,"该验证码已过期，请刷新验证码",null);
    }

    @ExceptionHandler(JsonProcessingException.class)
    public Result<?> handleJsonProcessingException(){
        return Result.fail(106,"账户存在异常，请联系维护人员",null);
    }

    @ExceptionHandler(JobNumberNotExistException.class)
    public Result<?> handleJobNumberNotExistException(){
        return Result.fail(108,"工号不存在",null);
    }

    @ExceptionHandler({UnsupportedEncodingException.class, MessagingException.class})
    public Result<?> handleMailSendException(){
        return Result.fail(111,"发送失败",null);
    }

    @ExceptionHandler({SignatureVerificationException.class, AlgorithmMismatchException.class, JWTDecodeException.class})
    public Result<?> handleParseJwtException(){
        return Result.fail(113,"token无效",null);
    }

    @ExceptionHandler(UserStatusInvalidException.class)
    public Result<?> handleUserStatusException(){return Result.fail(114,"用户已被删除，请联系管理员",null);}

}
